/**
 * SlipSwitch.java [V 1.0.0]
 * classes : com.xiaocq.iwordkiller.components.SlipSwitch
 * xiaocq Create at 2012-5-1 下午11:30:51
 */
package com.xiaocq.iwordkiller.components;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

/**
 * com.xiaocq.iwordkiller.components.SlipSwitch
 * 
 * @author Mystery Create at 2012-5-1 下午11:30:51
 */
public class SlipSwitch extends View implements OnTouchListener {
	// 开关开启时的背景，关闭时的背景，滑动按钮的背景
	private Bitmap switch_on_bg, switch_off_bg, slip_btn;
	// 开启时重绘区域，关闭时重绘区域
	private Rect on_rect, off_rect;
	// 是否正在滑动
	private boolean isSlipping = false;
	// 当前开关的状态 true-开启，false-关闭
	private boolean isSwitchOn = false;
	// 手指按下时的水平坐标与当前水平坐标
	private float touchX, currentX;

	// 开关监听器
	private OnSwitchListener onSwitchListener;
	// 是否设置了开关监听器
	private boolean hasSwitchListener = false;

	public SlipSwitch(Context context) {
		super(context);
		
		init();
	}

	public SlipSwitch(Context context, AttributeSet attrs) {
		super(context, attrs);

		init();
	}

	public SlipSwitch(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);

		init();
	}

	private void init() {
		setOnTouchListener(this);
	}

	public void setImageResource(int switch_bg, int slip) {
		switch_on_bg = BitmapFactory.decodeResource(getResources(), switch_bg);
		switch_off_bg = BitmapFactory.decodeResource(getResources(), switch_bg);
		slip_btn = BitmapFactory.decodeResource(getResources(), slip);

		// 右半边Rect，即滑动按钮在右半边时表示开关开启
		on_rect = new Rect(switch_off_bg.getWidth() - slip_btn.getWidth(), 0,
				switch_off_bg.getWidth(), slip_btn.getHeight());
		// 左半边Rect，即滑动按钮在左半边时表示开关关闭
		off_rect = new Rect(0, 0, slip_btn.getWidth(), slip_btn.getHeight());
	}

	public void setSwitchState(boolean switchState) {
		isSwitchOn = switchState;
	}

	public boolean getSwitchState() {
		return isSwitchOn;
	}

	public void updateSwitchState(boolean switchState) {
		isSwitchOn = switchState;
		invalidate();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		Matrix matrix = new Matrix();
		Paint paint = new Paint();

		// 滑动按钮左边的坐标
		float left_slipBtn;
		// 手指滑动到左半边的时候表示开关为关闭状态，滑动到右半边的时候表示开关为开启状态
		// if(currentX < switch_on_bg.getWidth() / 2){
		// canvas.drawBitmap(switch_off_bg, matrix, paint);
		// }else{
		// canvas.drawBitmap(switch_on_bg, matrix, paint);
		// }
		canvas.drawBitmap(switch_off_bg, matrix, paint);

		// 判断当前是否正在滑动
		if (isSlipping) {
			if (currentX > switch_on_bg.getWidth()) {
				left_slipBtn = switch_on_bg.getWidth() - slip_btn.getWidth();
			} else {
				left_slipBtn = currentX - slip_btn.getWidth() / 2;
			}
		} else {
			// 根据开关状态设置滑动按钮的位置
			if (isSwitchOn) {
				left_slipBtn = on_rect.left;
			} else {
				left_slipBtn = off_rect.left;
			}
		}

		// 对滑动按钮的位置进行异常判断
		if (left_slipBtn < 0) {
			left_slipBtn = 0;
		} else if (left_slipBtn > switch_on_bg.getWidth() - slip_btn.getWidth()) {
			left_slipBtn = switch_on_bg.getWidth() - slip_btn.getWidth();
		}

		canvas.drawBitmap(slip_btn, left_slipBtn, 0, paint);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		setMeasuredDimension(switch_on_bg.getWidth(), switch_on_bg.getHeight());
	}

	public boolean onTouch(View v, MotionEvent event) {
		switch (event.getAction()) {
		// 滑动
		case MotionEvent.ACTION_MOVE:
			currentX = event.getX();
			break;

		// 按下
		case MotionEvent.ACTION_DOWN:
			if (event.getX() > switch_off_bg.getWidth() || event.getY() > switch_off_bg.getHeight()) {
				return false;
			}

			isSlipping = true;
			touchX = event.getX();
			currentX = touchX;
			break;

		// 松开
		case MotionEvent.ACTION_UP:
			isSlipping = false;
			// 松开开关前的状态
			boolean previousSwitchState = isSwitchOn;

			if (event.getX() >= switch_on_bg.getWidth() / 2) {
				isSwitchOn = true;
			} else {
				isSwitchOn = false;
			}

			if (hasSwitchListener && (previousSwitchState) != isSwitchOn) {
				onSwitchListener.onSwitched(isSwitchOn);
			}
			break;
		default:
			break;

		}

		invalidate();
		return true;
	}

	public void setOnSwitchListener(OnSwitchListener listener) {
		onSwitchListener = listener;
		hasSwitchListener = true;
	}

	public interface OnSwitchListener {
		abstract void onSwitched(boolean isSwitchOn);
	}

}
